//
//  HomeItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/3.
//  ai日记首页
//

import SwiftUI

struct HomeAiView: View {
    var item: HomeTableItem
    @StateObject var viewModel : HomeAiViewModel

    init(item: HomeTableItem) {
        self.item = item
        _viewModel = StateObject(wrappedValue: HomeAiViewModel(item: item))
    }
    
    var body: some View {
        ZStack{
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
            } else {
                ScrollView {
                    //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                    LazyVStack(spacing: 10) {
                        ForEach(viewModel.items,id: \.objectId) { item in
                            DiaryItemView(bean: item)
                        }
                        LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                            viewModel.getData(false, item: item)
                        }) {
                            LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing)
                        }
                        .noMore(viewModel.noMore)
                        .preload(offset: 10)
                    }
                    
                }
                .enableRefresh()
            }
        }
        .onAppear{
            if !viewModel.isInit {
                viewModel.getData(true,item : item)
                viewModel.isInit = true
            }
        }
    }
}

#Preview {
//    HomeAiView(item: HomeTableItem(id: "", name: "ai日记"))
}
